/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.impl;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;

/**
 * @author stl
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public abstract class BaseMapShapeImpl implements MapShape, MapShapes {

	public static final Color DEFAULT_COLOR =Color.BLACK;
	
	private static final Color HANDLE_COLOR =Color.BLACK;
	private static final double HANDLE_SQUARE_SIZE =8;	
	
	private String id =null;
	private int type =UNDEFINED;
	private String layer =null;	
	private boolean selected =false;
	private int selectedHandleIndex =-1;
	
	private Color color =DEFAULT_COLOR;
	private Color fillColor =null;
	
	protected Rectangle2D bounds;
	private double zoomFactor =1;
	private double mapFactor =1;
	private int editMode;
		
	/**
	 * @return
	 */
	public String getId() {
		return id;
	}	
	
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return
	 */
	public int getType() {
		return type;
	}

	/**
	 * @param i
	 */
	public void setType(int i) {
		type = i;
	}	
	
	public void setEditMode( int editMode ) {
		this.editMode =editMode;
	}
	
	public int getEditMode() {
		return editMode;
	}

	public boolean isSelected() {
		return selected;
	}

	public void select(boolean sel) {
		this.selected =sel;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color =color;	
	}

	public Color getFillColor() {
		return fillColor;
	}

	public void setFillColor(Color color) {
		this.fillColor =color;		
	}
	
	public Rectangle2D getBounds() {
	    return bounds;
	}	
	
	/**
	 * @return
	 */
	public String getLayer() {
		return layer;
	}

	/**
	 * @param string
	 */
	public void setLayer(String string) {
		layer = string;
	}	
	
	/**
	 * @return
	 */
	public int getSelectedHandleIndex() {
		return selectedHandleIndex;
	}

	/**
	 * @param point
	 */
	public void setSelectedHandleIndex(int idx) {
		selectedHandleIndex = idx;
	}
	
	protected Rectangle2D.Double getHandleRectangle( double x, double y ) {
	    double factor =getScreenToMapFactor();
	    
		return new Rectangle2D.Double(x-(HANDLE_SQUARE_SIZE*factor/2d), 
		        					  y-(HANDLE_SQUARE_SIZE*factor/2d), 
		        					  HANDLE_SQUARE_SIZE*factor, 
		        					  HANDLE_SQUARE_SIZE*factor);
	}
	
	protected void displayHandleRectangle( Graphics2D g2D, double x, double y ) {
		g2D.setColor( HANDLE_COLOR );
		g2D.draw( getHandleRectangle( x, y ) );
	}	
		
    /**
     * @return Returns the mapFactor.
     */
    public double getMapFactor() {
        return mapFactor;
    }
    
    /**
     * @param mapFactor The mapFactor to set.
     */
    public void setMapFactor(double mapFactor) {
        this.mapFactor = mapFactor;
    }
    
    /**
     * @return Returns the zoomFactor.
     */
    public double getZoomFactor() {
        return zoomFactor;
    }
    
    /**
     * @param zoomFactor The zoomFactor to set.
     */
    public void setZoomFactor(double zoomFactor) {
        this.zoomFactor = zoomFactor;
    }
    
    /**
     * I hate to have this one in here
     * It should remain in mtk
     * @return
     */
	protected double getMapToScreenFactor() {
	    return mapFactor * (double)zoomFactor / 100d;
	}

    /**
     * I hate to have this one in here
     * It should remain in mtk
     * @return
     */
	protected double getScreenToMapFactor() {
	    return 1d / getMapToScreenFactor();
	}    

}
